package com.bjfu.mapper;

import com.bjfu.model.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {

   /* // 插入一篇新的文章
    @Insert("INSERT INTO article (title, summary, content, attachment_link, publish_date, team_id) VALUES (#{title}, #{summary}, #{content}, #{attachmentLink}, #{publishDate}, #{teamId})")
    @Options(useGeneratedKeys = true, keyProperty = "articleId", keyColumn = "article_id")
    void insertArticle(Article article);

    // 更新文章信息
    @Update("UPDATE article SET title = #{title}, summary = #{summary}, content = #{content}, attachment_link = #{attachmentLink}, publish_date = #{publishDate}, team_id = #{teamId} WHERE article_id = #{articleId}")
    void updateArticle(Article article);

    // 删除文章
    @Delete("DELETE FROM article WHERE article_id = #{articleId}")
    void deleteArticle(Long articleId);*/

    // 根据 article_id 获取文章信息
    @Select("SELECT * FROM article WHERE article_id = #{article_id}")
    Article getArticleById(Long article_id);

    // 获取所有文章
    @Select("SELECT * FROM article")
    List<Article> getArticles();

    // 根据团队 ID 获取该团队发布的所有文章
    @Select("SELECT * FROM article WHERE team_id = #{teamId}")
    List<Article> getArticlesByTeamId(Long teamId);

    @Update("UPDATE article SET status = #{pass_status} WHERE article_id = #{article_id}")
    int judgeArticle(Long article_id, Integer pass_status);

    // 查询所有待审核的文章
    @Select("SELECT * FROM article WHERE status = 1")  //1是待审核的状态
    List<Article> getArticlesNotJudged();

    // 查询指定类别且状态为2的文章
    @Select("SELECT * FROM article WHERE status = 2 AND category = #{category}")
    List<Article> getArticlesByCategory(Integer category);


    // 插入一个新的文章
    @Insert("INSERT INTO article (title, category, summary, content, attachment_link, publish_date, status, team_id) " +
            "VALUES (#{title}, #{category}, #{summary}, #{content}, #{attachmentLink}, #{publishDate}, #{status}, #{teamId})")
    @Options(useGeneratedKeys = true, keyProperty = "articleId", keyColumn = "article_id")
    int insertArticle(Article article);


    // 更新文章信息，只有公开的文章才能更新 增加为失败也可以
    @Update("UPDATE article " +
            "SET title = #{title}, category = #{category}, summary = #{summary}, content = #{content}, " +
            "attachment_link = #{attachmentLink}, publish_date = #{publishDate}, status = #{status}, team_id = #{teamId} " +
            "WHERE article_id = #{articleId} AND (status =2 OR status=3)")
    int updateArticle(Article article);

    // 删除文章
    @Delete("DELETE FROM article WHERE article_id = #{articleId}")
    int deleteArticle(Long articleId);


    @Select("SELECT * FROM article")
    List<Article> getALLArticles();
}
